package com.jah.picavue.client.view;

import com.jah.picavue.client.model.Photo;
import com.jah.picavue.client.EventBus;
import com.jah.picavue.client.events.AlbumSelectionEvent;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.jah.picavue.client.Config;

/**
 * A composite of an Image and a Label for the Image Caption.
 */

public class Thumbnail extends Composite implements ClickHandler, Config {

	private Image thumb_image = new Image();
	private Label thumbcaption = new Label();
	final private PopupPanel pop = new PopupPanel(true);
	private Image photoImage = new Image();
	private Photo photoData;
	private double width;
	private double height;
	private final String parentID;
	private final String context;

	public Thumbnail(String id, String image_url, String caption, String view) {

		this.width = THUMBSIZE;
		this.height = THUMBSIZE + 40;

		parentID = id;
		context = view;

		String mount_width = (new Integer((int) this.width)).toString() + "px";
		String mount_height = (new Integer((int) this.height)).toString() + "px";

		VerticalPanel panel = new VerticalPanel();
		panel.setSize(mount_width, mount_height);
		panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel.add(thumb_image);
		thumb_image.setUrl(image_url);
		thumb_image.addClickHandler(this);
		panel.add(thumbcaption);
		thumbcaption.setText(caption);
		thumbcaption.setSize(mount_width, "30px");

		// All composites must call initWidget() in their constructors.
		initWidget(panel);
	}

	public void onClick(ClickEvent event) {
		if (context.equals("gallery")) {
			Object sender = event.getSource();
			if (sender == thumb_image) {
				photoImage.setUrl(photoData.getContent());
				pop.setWidget(photoImage);
				pop.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
					public void setPosition(int offsetWidth, int offsetHeight) {
						photoData.resetDims();
						while (true) {
							int left = ((Window.getClientWidth() - photoData.getWidth().intValue()) / 2) -5;
							int top = ((Window.getClientHeight() - photoData.getHeight().intValue()) / 2) -5;
							int new_width = 0;
							int new_height = 0;
							if (left < 0){
								// image is wider than window - scale it.
								new_width = (2*left) + photoData.getWidth().intValue() - 30; // 30px less than the window
								new_height = (int)((new_width) * (photoData.getHeight() / photoData.getWidth()));
								if (left < 0 && new_height > (Window.getClientHeight() - 30)) {
									// scaled image is even now too tall
									new_height = Window.getClientHeight() - 30;
									new_width = (int)((new_height) * (photoData.getWidth() / photoData.getHeight()));
								}
							}
							if (new_height == 0 && top < 0){
								// image has not been modded yet, but it is too tall - scale it.
								new_height = (2*top) + photoData.getHeight().intValue() -30; // 20px less than the window
								new_width = (int)((new_height) * (photoData.getWidth() / photoData.getHeight()));
								if ( new_width > (Window.getClientWidth() - 30)) {
									// scaled image is even now too wide
									new_width = Window.getClientWidth() - 30;
									new_height = (int)((new_width) * (photoData.getHeight() / photoData.getWidth()));
								}
							}
							if (new_height != 0 && new_width != 0) {
								photoData.setWidth((double) new_width);
								photoData.setHeight((double) new_height);

								continue; // recalculate left and top based on the new image size
							}
							// OK all's well.
							photoImage.setSize(photoData.getWidth().toString(), photoData.getHeight().toString());
							pop.setPopupPosition(left+Window.getScrollLeft(), top+Window.getScrollTop());
							break;
						}
					}
				});
			} else if (sender == thumbcaption) {
				// show actions
			} else if (sender == photoImage) {
				pop.hide();
			}
			// TODO Slide show starting here event
		} else {
			// TODO Actions Overlay
			// TODO Slideshow event
			AlbumSelectionEvent evt = new AlbumSelectionEvent();
			evt.setView("gallery");
			evt.setSelectedAlbumID(parentID);
			EventBus.get().fireEvent(evt);
		}
	}

	public void setPhoto(Photo photo) {
		this.photoData = photo;
		this.photoImage.setUrl(this.photoData.getContent());
		this.photoImage.addClickHandler(this);
	}

	public double getWidth() {
		return this.width;
	}

	public double getHeight() {
		return this.height;
	}
}